/*
    This file is part of MP3DecodeDLL for Symbian.

    MP3DecodeDLL for Symbian
    Copyright (C) 2004 Denis Mingulov <denis@mingulov.com>

    MP3DecodeDLL for Symbian is free software; you can redistribute
    it and/or modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2 of
    the License, or (at your option) any later version.

    MP3DecodeDLL for Symbian is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied
    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MP3DecodeDLL for Symbian; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#ifndef MP3DECODE_H
#define MP3DECODE_H

#include <e32std.h>
//#include "logfile.h"
#include <f32file.h>
//#include <charconv.h>

#include "mad.h"

//#include <mdaaudiooutputstream.h>
//#include <mda\\common\\audio.h>
//TMdaAudioDataSettings
//#include <stdio.h>
//#include <string.h>


#define MAX_RESAMPLEFACTOR     6
#define MAX_NSAMPLES		(1152 * MAX_RESAMPLEFACTOR)

class CMP3Decode : CBase
{
struct resample_state {
  mad_fixed_t ratio;

  mad_fixed_t step;
  mad_fixed_t last;
};

public:
	static CMP3Decode *NewL ();
	static CMP3Decode *NewLC ();
	virtual ~CMP3Decode();

	TInt Open(RFs &aFileServer, const TDesC &aFileName, int mediaoffset = 0, int mediarealsize = 0); // opens the stream
	TInt Close();
	TInt SetEqualizer (TInt aEq60, TInt aEq170, TInt aEq310, TInt aEq600, TInt aEq1K, TInt aEq3K, TInt aEq6K, TInt aEq12K, TInt aEq14K, TInt aEq16K, TInt aEqZero, TInt aEqDiv);
	TInt SetResampleRate(TInt aResampleRate);
	TInt DecodeOneFrame (TDes &aBuffer);
	TInt GetFileLength();
	TInt GetFilePosition();

protected:
	TInt CloseMP3 ();
	TInt InitMP3 ();

	int DecodeOneFrameLow ();
	unsigned int resample_block(struct resample_state *state, unsigned int nsamples, mad_fixed_t const *old, TInt16 *newb);
	int resample_init (struct resample_state *state, unsigned int oldrate, unsigned int newrate);
	signed int MP3FixedToShort (mad_fixed_t sample);
	void ApplyFilter(struct mad_frame *Frame);

	// this method fills the buffer and writes it into the stream
//	void UpdateBuffer();

	// this methoed needs to implemented by YOU
	// void FillBuffer(TInt16* buffer, TUint len);
//	TInt FillBuffer();

// we will also need these...
protected:
//	TInt16* iSoundData; // sound buffer
//	TPtr8* iSoundBuf; // descriptor for using our soundbuffer

	TBool bFileOpened;

private:
	CMP3Decode();
	void ConstructL ();

	TFileName strFileNameMP3;
	RFs *iFileSession;
	RFile iFile;

	// mp3
	struct mad_stream	Stream;
	struct mad_frame	Frame;
	struct mad_synth	Synth;
	mad_timer_t			Timer;
	int FrameCount;
	int Status;
	int iFileCurPos;
	int iMediaOffset;//william
	int iMediaRealSize;//william
//	int iDecoding;
//	int iDecoded;

#define INPUT_BUFFER_SIZE 10000
#define OUTPUT_BUFFER_SIZE 8192
	int iOutputBufferCurrent;
	int	iOutputBufferLength[2];
	unsigned char InputBuffer[INPUT_BUFFER_SIZE+MAD_BUFFER_GUARD];
	unsigned char InputBuffer2[INPUT_BUFFER_SIZE+MAD_BUFFER_GUARD];
	TPtr8 *iInputBuf;
	TInt16 OutputBuffer[2][OUTPUT_BUFFER_SIZE];
//	TPtr8* iSoundBuf2[2]; // descriptor for using our soundbuffer
	mad_fixed_t iEqualizer[32];
	TInt iResampleRate;
};

#endif
